package com.own.business.pay.module.alipay.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.own.business.pay.common.exception.PayException;
import com.own.business.pay.core.model.PayOrderModel;
import com.own.business.pay.core.model.PayResultModel;
import com.own.business.pay.module.alipay.property.AliPayProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * AlipayUtil
 *
 * @author chenxueli
 * @date 2023-08-17 15:44:00
 */
@Slf4j
@Component
public class AlipayUtil {

    @Resource
    private AliPayProperty aliPayProperty;

    /**
     * app支付
     *
     * @param client        客户端
     * @param payOrderModel 支付订单
     * @return 支付凭证
     */
    public PayResultModel app(DefaultAlipayClient client, PayOrderModel payOrderModel) {
        // 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        var request = new AlipayTradeAppPayRequest();
        // SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        var model = new AlipayTradeAppPayModel();
        model.setBody(payOrderModel.getBody());
        model.setSubject(payOrderModel.getSubject());
        model.setOutTradeNo(payOrderModel.getTradeNo());
        model.setTimeoutExpress(payOrderModel.getTimeoutExpress());
        // 金额单位为元
        model.setTotalAmount(new BigDecimal(payOrderModel.getTotalAmount()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString());
        model.setProductCode("QUICK_MSECURITY_PAY");
        // request信息设置
        request.setBizModel(model);
        request.setNotifyUrl(aliPayProperty.getNotifyUrl());
        try {
            // 这里和普通的接口调用不同，使用的是sdkExecute
            var response = client.sdkExecute(request);
            if (response.isSuccess()) {
                // 就是orderString 可以直接给客户端请求，无需再做处理。
                return new PayResultModel(payOrderModel.getTradeNo(), response.getBody());
            } else {
                log.error("支付宝支付失败，错误信息：{}", response.getMsg());
                throw new PayException("支付宝支付失败");
            }
        } catch (AlipayApiException e) {
            throw new PayException(e);
        }
    }

    /**
     * 二维码支付
     *
     * @param client        客户端
     * @param payOrderModel 支付订单
     * @return 支付凭证
     */
    public PayResultModel qrcode(DefaultAlipayClient client, PayOrderModel payOrderModel) {
        // 创建API对应的request
        var request = new AlipayTradePrecreateRequest();
        // 设置请求参数
        var model = new AlipayTradePrecreateModel();
        model.setBody(payOrderModel.getBody());
        model.setSubject(payOrderModel.getSubject());
        model.setOutTradeNo(payOrderModel.getTradeNo());
        model.setTimeoutExpress(payOrderModel.getTimeoutExpress());
        // 金额单位为元
        model.setTotalAmount(new BigDecimal(payOrderModel.getTotalAmount()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString());
        // request信息设置
        request.setBizModel(model);
        request.setNotifyUrl(aliPayProperty.getNotifyUrl());
        try {
            // 发起请求，获取支付宝支付二维码链接
            var response = client.execute(request);
            if (response.isSuccess()) {
                return new PayResultModel(payOrderModel.getTradeNo(), response.getQrCode());
            } else {
                log.error("支付宝支付失败，错误信息：{}", response.getMsg());
                throw new PayException("支付宝支付失败");
            }
        } catch (AlipayApiException e) {
            throw new PayException(e);
        }
    }

}
